package com.tl.khadmin.controller;
  
import java.util.Map;

import javax.annotation.Resource; 
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
  
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tl.khadmin.bean.Product;
import com.tl.khadmin.bean.ProductSupply;
import com.tl.khadmin.bean.dto.ProductSupplyDto;
import com.tl.khadmin.bean.dto.ProductSupplyListDto;
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.ProductSupplyIService;
import com.tl.khadmin.util.CommonUtil;

@RestController
@RequestMapping(value = "/productsupply")
public class ProductSupplyController {
	
	@Resource
	private ProductSupplyIService productSupplyService; 
	
	/**
	 * 审核
	 * @param session 
	 * @return
	 */
	@RequestMapping(value = "/audit") 
	@ResponseBody
	public String audit(HttpSession session, ProductSupplyDto productSupplyDto) {
		return productSupplyService.audit(session, productSupplyDto);
	}
	
	@RequestMapping(value = "/querybyid")
	@ResponseBody
	public Map<String, Object> queryById(HttpSession session, Long id) {
		if(id==null){
			return null;
		}
		return productSupplyService.queryProductById(id);
	}

	@RequestMapping(value = "/showproductSupplybypage")
	public Map<String, Page<ProductSupplyListDto>> showProductSupplyByPage(HttpSession session, @RequestParam Map<String, Object> params) { 
		Integer pageNum = Integer.parseInt(params.get("pageNum").toString());
		Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
		return CommonUtil.returnResultCodeJson("result", productSupplyService.showProductSupplyByPage(session, pageNum, pageSize, params));
	}
	
	@RequestMapping(value="/uploaddeclarationimg")  
	@ResponseBody
	public Map<String, String> upLoadDeclarationImg(HttpSession session, @RequestParam("file") MultipartFile file){
		String path = session.getServletContext().getRealPath("/upload/productimg");
		return productSupplyService.upLoadDeclarationImg(path,file); 
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String saveProductSupply(HttpSession session, @Valid ProductSupplyDto productSupplyDto, BindingResult br) {
		return productSupplyService.saveProductSupply(session, productSupplyDto);
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public String updateProductSupply(HttpSession session, @Valid ProductSupplyDto productSupplyDto, BindingResult br) {
		return productSupplyService.updateProductSupply(session, productSupplyDto);
	}
	
	@RequestMapping(value = "/updatesalestatus")
	@ResponseBody
	public String updateShelveState(Long id,Integer saleStatus) {
		if(id==null || saleStatus==null ){
			return CommonUtil.returnMapResultByRows(0, "","参数错误，请重试");
		}
		int ret = productSupplyService.updateSaleState(id,saleStatus);
		return CommonUtil.returnMapResultByRows(ret, "修改成功","修改失败");
	}


}
